package com.fsh.oss.controller;

import com.fsh.oss.entity.Result;
import com.fsh.oss.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping
@Slf4j
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 创建桶
     */
    @RequestMapping("/createBucket")
    public void createBucket(String bucketName){
        fileService.createBucket(bucketName);
    }

    @RequestMapping("/testGetAllBuckets")
    public List<String> testGetAllBuckets(){
        List<String> allBuckets = fileService.getAllBuckets();
        return allBuckets;
    }


    @RequestMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) throws Exception {
        return fileService.getUrl(bucketName, objectName);
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
        String url = fileService.uploadFile(uploadFile, bucket, objectName);
        if(log.isInfoEnabled()){
            log.info("FileController.class-upload()-upload:{},objectName:{},getOriginalFilename():{}",bucket,objectName,uploadFile.getOriginalFilename());
            log.info("url---:{}",url);
        }
        return Result.ok(url);
    }

}
